<?php
/**
 * 前端文件缓存服务器
 *
 * @author xyma
 *
 */
class VarnishHelper
{
	/**
	 * 清除缓存的存储文件
	 */
	static function purgeStock($key)
	{
		$servs	=	app_cfg('imgcfg', 'imgserver');
		if($servs) foreach($servs as $sv)
		{
			self::purge($sv, "/{$key}/");
		}
		return true;
	}
	
	/**
	 * 连接服务器，清除缓存文件
	 */
	static function purge($host, $url)
	{
		$fp		=	null;
		$errmsg	=	'';
		$errno	=	'';
		
		try{
			if(false!==strpos($host,':')){
				list($ip,$port,)	=	explode(':',$host);
				$fp	=	fsockopen($ip, $port, $errmsg, $errno, 2);
			}
			else{
				$fp	=	fsockopen($host, 80, $errmsg, $errno, 2);
			}
			if(!$fp){
				return false;
			}
		}
		catch(Exception $_){
			return false;
		}
		
		$cmd	=	"PURGE {$url} HTTP/1.1\r\n";
		$cmd	.=	"Host: {$host}\r\n";
		$cmd	.=	"Connection: close\r\n\r\n";
		fwrite($fp, $cmd);
		fread($fp, 4096);
		fclose($fp);
		
		return true;
	}
	
}

?>